/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package caa.struts.confirmclass.action;

import caa.struts.confirmclass.form.ManageConfirmClassForm;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import th.com.entity.ConfirmclassEntity;
import th.com.entity.MakeupclassEntity;
import th.com.entity.ScheduleEntity;
import th.com.entity.SemesterEntity;
import th.com.entity.UserEntity;
import th.com.ku.CAAService;


/**
 *
 * @author Ketsaraporn
 */
public class ConfirmClassManageAction extends DispatchAction {

    public ActionForward doManageConfirmclass(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageConfirmClassForm confirmClassForm = (ManageConfirmClassForm) form;
        
        ArrayList<SemesterEntity> list = CAAService.findConfirmClass(confirmClassForm.getKwKeyWord());
        confirmClassForm.setListSearch(list);

        return mapping.findForward("manageconfirmclass.success");
    }

    public ActionForward doAddConfirmClass(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageConfirmClassForm confirmClassForm = (ManageConfirmClassForm) form;
        
        ScheduleEntity scheduleEntity = new ScheduleEntity();
        scheduleEntity.setCouId(confirmClassForm.getCouId());
        confirmClassForm.setListSchedule(CAAService.findSchedule(scheduleEntity));
        confirmClassForm.setAccess(new String[confirmClassForm.getListSchedule().size()]);

        return mapping.findForward("addconfirmclass.success");

    }
    
    public ActionForward doCreate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageConfirmClassForm confirmClassForm = (ManageConfirmClassForm) form;
        String userId = ((UserEntity)request.getSession().getAttribute("isUsername")).getUseId();
        
        for(int i = 0;i < confirmClassForm.getAccess().length;i++) {
            if(confirmClassForm.getAccess()[i] != null) {
                if(confirmClassForm.getAccess()[i].equalsIgnoreCase("1")) {
                    ConfirmclassEntity entity = new ConfirmclassEntity();
                    entity.setSchID(confirmClassForm.getListSchedule().get(i).getSchId());
                    entity.setConfCreatedBy(userId);
                    
                    CAAService.createConfirmclass(entity);
                } else if(confirmClassForm.getAccess()[i].equalsIgnoreCase("0")) {
                    MakeupclassEntity entity = new MakeupclassEntity();
                    entity.setSchId(confirmClassForm.getListSchedule().get(i).getSchId());
                    entity.setMarcCreatedBy(userId);
                    
                    CAAService.createMakeupclass(entity);
                }
            }
//            System.out.println(i+" "+confirmClassForm.getAccess()[i]);
        }
        
//        confirmClassForm.setListSchedule(new ArrayList<ScheduleEntity>());
//        confirmClassForm.setAccess(new String[confirmClassForm.getListSchedule().size()]);

        return mapping.findForward("manageconfirmclass.success");

    }
    
    public ActionForward goBack(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageConfirmClassForm confirmClassForm = (ManageConfirmClassForm) form;
        
        confirmClassForm.setListSchedule(new ArrayList<ScheduleEntity>());
        confirmClassForm.setAccess(new String[confirmClassForm.getListSchedule().size()]);

        return mapping.findForward("manageconfirmclass.success");

    }
    
    
}
